import Vue from "vue"
import Vuex from "vuex"
Vue.use(Vuex);

let state = {
  flags:false,
  bool:true,
  numb:'1111111111111111',
  number:60,
    good: {
        baseInfo: {
            pics: []
        },
        detail: '',
        rates: {
            mpcList: {
                listObj: []
            }
        },
        activeIndex: 0,
        showCnt: false
    },
    cartItems:[],
    checkAll: true
};
let mutations = {
    getLogin(state,msg){
      state.numb=msg;
    },
    flagChange(state){
      state.flags = true;
    },
    flagCh(state){
      state.flags = false;
    },
    boolChange(state){
      state.bool = false;
    },
    getGoodDetail({good},res) {
        res.baseInfo.then(res => {
            return res.json();
        }).then(res => {
            good.baseInfo = res.data[0];
        });
        res.detail.then(res => {
            return res.text();
        }).then(res => {
            good.detail = res;
        });
        res.rates.then(res => {
            return res.json();
        }).then(res => {
            good.rates = res.data;
        })
    },
    setActiveIndex({good},index) {
        good.activeIndex = index;
    },
    closeCnt({good}) {
        good.showCnt = false;
    },
    showCnt({good}) {
        good.showCnt = true;
    },
    addItem(state,newItem,count){
        if (count===undefined) {
            count = 1;
        }
        let flag = false;
        state.cartItems.map(function(item,index) {
           if(item.productId==newItem.productId){
                flag=true;
                newItem.count += count;
                console.log(newItem.count)
            }
        });
        if(!flag){
            Vue.set(newItem,'count',1);
            Vue.set(newItem,'check',true);
            state.cartItems.push(newItem);
        }
    },
    checkItem(state,item){
       item.check=!item.check;
       state.checkAll = !state.cartItems.some(function(item) {
            return item.check==false;
        });
    },
    checkAll(state){
        state.checkAll = !state.checkAll;
        state.cartItems.map(function(item){
            item.check = state.checkAll;
        })
    },
    delItem(state){
        state.cartItems = state.cartItems.filter(function (item,index,arr) {
            return item.check==false;
        })
    },
    add(state,item) {
        item.count++;
    },
    reduce(state,item) {
        if (item.count>1) {
            item.count--;
        }
    },
    num(state){
      let timeId = setInterval(() => {
        state.number--;
        if (state.number == 0) {
          clearInterval(timeId)
        }
        console.log(state.number)
      },1000)
      console.log(state.number)
    }
};
let actions = {
    getGoodDetail({commit}) {
        var res = {
            baseInfo: fetch("static/data/goodDetail/baseInfo.json"),
            detail: fetch("static/data/goodDetail/desc.html"),
            rates: fetch("static/data/goodDetail/getRate.json")
        };
        commit('getGoodDetail',res);
    }
};
let getters = {
   getCartCount(state) {
       let count = 0;
        state.cartItems.forEach(function(item) {
            count += item.count;
        });
        return count;
   },
   getMoney(state) {
       let money = 0;
       state.cartItems.forEach(function (item) {
           if(item.check) {
               money += item.count*item.salePrice;
           }
       })
       return money.toFixed(2);
   }
}

export default new Vuex.Store({
    state, mutations, actions, getters
})
